/*
Spotter Exercise Manager
Copyright 2011 Evan Nelson.

This file is part of Spotter Exercise Manager.

Spotter Exercise Manager is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Spotter Exercise Manager is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Spotter Exercise Manager.  If not, see <http://www.gnu.org/licenses/>.

The author can be contacted by emailing ean5533@gmail.com
*/

#include <QApplication>
#include <QDesktopWidget>
#include <QMessageBox>

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include "database.h"

const QString MainWindow::baseTitle = "Exercise Manager";

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindowClass)
{
    ui->setupUi(this);

    Database::openDatabase();

    //Force all tabs/stacked widgets to start on the beginning pages, regardless of what the .ui file set them as
    this->on_navMain_currentChanged(0);
    ui->navMain->setCurrentIndex(0);
    ui->stackedWidgetMain->setCurrentIndex(0);
    ui->listWeightlifting->setCurrentRow(0);
    ui->listEndurance->setCurrentRow(0);
    ui->listEndurance->setCurrentRow(0);
    ui->listBodyStats->setCurrentRow(0);

    //set up weightlifting page
    sectionWeightlifting = new SectionWeightlifting;
    ui->pageWeightlifting->layout()->addWidget(sectionWeightlifting);
    connect(ui->listWeightlifting, SIGNAL(currentRowChanged(int)), sectionWeightlifting->tabsMain, SLOT(setCurrentIndex(int)));

    //set up endurance page
    sectionEndurance = new SectionEndurance;
    ui->pageEndurance->layout()->addWidget(sectionEndurance);
    connect(ui->listEndurance, SIGNAL(currentRowChanged(int)), sectionEndurance->tabsMain, SLOT(setCurrentIndex(int)));

    //set up body stats page
    sectionBodyStats = new SectionBodyStats;
    ui->pageBodyStats->layout()->addWidget(sectionBodyStats);
    connect(ui->listBodyStats, SIGNAL(currentRowChanged(int)), sectionBodyStats->tabsMain, SLOT(setCurrentIndex(int)));

    //set up system page
    sectionSystem = new SectionSystem;
    ui->pageSystem->layout()->addWidget(sectionSystem);
    connect(ui->listSystem, SIGNAL(currentRowChanged(int)), sectionSystem->tabsMain, SLOT(setCurrentIndex(int)));

    //center the window on the active desktop
    QRect rect = QApplication::desktop()->availableGeometry(this);
    this->move(rect.center() - this->rect().center());
}

MainWindow::~MainWindow()
{
    Database::closeDatabase();
    delete ui;
}

void MainWindow::on_actionQuit_triggered() {
    exit(0);
}

void MainWindow::on_actionManual_triggered() {
    QMessageBox("Manual", "We don't have a manual yet :-(  Sorry", QMessageBox::Information,
                QMessageBox::Ok, QMessageBox::NoButton, QMessageBox::NoButton, this).exec();

}

void MainWindow::on_actionAbout_triggered() {
    QMessageBox("About Spotter", "Spotter Exercise Manager\n\nCopyright 2011 Evan Nelson", QMessageBox::Information,
                QMessageBox::Ok, QMessageBox::NoButton, QMessageBox::NoButton, this).exec();
}

void MainWindow::on_navMain_currentChanged(int index)
{
    this->setWindowTitle(MainWindow::baseTitle + " - " + ui->navMain->itemText(index));
}
